package com.web.graduationpro.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.dao.service.ApplyService;
import com.web.graduationpro.dao.service.MemberService;
import com.web.graduationpro.dao.service.OrderService;
import com.web.graduationpro.model.Apply;
import com.web.graduationpro.model.Member;
import com.web.graduationpro.model.Order;
import com.web.graduationpro.util.PageUtil;

@Controller
@RequestMapping("/usercenterapply/*")
public class UserCenterApplyController {

	@Resource(name="memberServiceImpl")
	private MemberService memberServiceImpl;
	
	@Resource(name="orderServiceImpl")
	private OrderService orderServiceImpl;
	
	@Resource(name="applyServiceImpl")
	private ApplyService applyServiceImpl;
	
	@RequestMapping(value = "/myapplylist", method = RequestMethod.GET)  
    public String myapplylist(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			/*************************分页开始****************************/
			String actionUrl = "/GraduationPro/usercenterapply/myapplylist.do?";
			int totalNum = 0;
			int showNum = 5;
			String currPageStr = request.getParameter("currPage");
			String pageSizeStr = request.getParameter("pageSize");
			int currPage = 1;
			if(currPageStr!=null && !"".equals(currPageStr.trim())){
				currPage = Integer.parseInt(currPageStr);
			}
			int pageSize = 10;
			if(pageSizeStr!=null && !"".equals(pageSizeStr.trim())){
				pageSize = Integer.parseInt(pageSizeStr);
			}
			String queryCountHql = "SELECT count(*) From Apply a WHERE a.id!=0 and a.member.id="+loginUser;
			totalNum = applyServiceImpl.findTotalCount(queryCountHql);
			List<String> page = null;
			if(totalNum>0){
				page = PageUtil.getInstance().getPage(actionUrl, totalNum, showNum, currPage, pageSize);
			}
			request.setAttribute("page", page);
			/*************************分页结束****************************/
			
			/*************************查询数据开始****************************/
			String hql = "  FROM Apply a WHERE a.id!=0 and a.member.id="+loginUser;
			List<Apply> myApplys = applyServiceImpl.findByPage(hql, ((currPage-1)*pageSize), pageSize);
			if(myApplys!=null && myApplys.size()!=0){
				request.setAttribute("myApplys", myApplys);
			}
			/*************************查询数据结束****************************/
			
			return "/user/myapply";
		}else{
			return "/login";
		}
    } 
	
	@RequestMapping(value = "/myapplyinfo", method = RequestMethod.GET)  
    public String myapplyinfo(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			String id = request.getParameter("id");
			if(id!=null && !"".equals(id.trim())){
				Apply apply = (Apply) applyServiceImpl.findById(Order.class, Integer.parseInt(id));
				if(apply!=null){
					request.setAttribute("apply", apply);
					return "/user/myapplyinfo";
				}
			}
			return myapplylist(request,reponse);
		}else{
			return "/login";
		}
    } 
}
